{include file="public/toper" /}
<div class="layui-tab layui-tab-brief main-tab-container">
    <ul class="layui-tab-title main-tab-title">
        {foreach name="models" id="model"}
            <li {if condition="$model['tablename'] eq 'link'"}class="layui-this"{/if}><a href="{$model.url}">{$model.name} </a></li>
        {/foreach}
        <span class="main-tab-item">链接管理</span>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <form class="layui-form">
                <input type="hidden" name="category_id" value="">
                <table class="list-table">
                    <thead>
                    <tr>
                        <th style="min-width:25px">ID</th>
                        <th>链接</th>
                        <th>所属栏目</th>
                        <th class="can_click">
                            添加时间
                        </th>
                        <th style="width:90px">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                        {notempty name="data"}
                            {foreach name="data" id="da"}
                                <tr>
                                    <td>{$da['id']}</td>
                                    <td>
                                        <a class="list-title" href="{$da['url']}"
                                           target="_blank">{$da['url']}</a>
                                    </td>
                                    <td>{$da['name']}</td>
                                    <td>{$da['create_time']}</td>
                                    <td style="text-align: center;">
                                        <a href="{:url('admin/category/edit',array('parent'=>$da['id']))}"
                                           class="layui-btn layui-btn-sm" title="编辑" style="float: left"><i class="layui-icon"></i></a>
                                    </td>
                                </tr>
                            {/foreach}
                        {/notempty}
                    </tbody>

                </table>
            </form>

        </div>
    </div>
</div>

<script type="text/javascript">
    layui.use(['element', 'layer', 'form' ,'upload'], function(){
        var element = layui.element
                ,jq = layui.$
                ,form = layui.form
                ,laypage = layui.laypage
                ,upload = layui.upload;


        //推荐
        jq('.is_recommend').click(function(){
            var id = jq(this).attr('data-id');
            loading = layer.load(2, {
                shade: [0.2,'#000'] //0.2透明度的白色背景
            });
            var row = jq(this);
            jq.post('{:url("article/to_recommend")}',{'id':id},function(data){
                if(data.code == 200){
                    if(row.hasClass('list-switch-off')){
                        row.removeClass('list-switch-off').find('.layui-icon').html('&#xe605;');
                        row.attr('title','点击开启');
                    }else{
                        row.addClass('list-switch-off').find('.layui-icon').html('&#x1006;');
                        row.attr('title','点击关闭');
                    }
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 1, time: 1000});
                }else{
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                }
            });
        });
        //置顶
        jq('.is_top').click(function(){
            var id = jq(this).attr('data-id');
            loading = layer.load(2, {
                shade: [0.2,'#000'] //0.2透明度的白色背景
            });
            var row = jq(this);
            jq.post('{:url("article/to_top")}',{'id':id},function(data){
                if(data.code == 200){
                    if(row.hasClass('list-switch-off')){
                        row.removeClass('list-switch-off').find('.layui-icon').html('&#xe605;');
                        row.attr('title','点击开启');
                    }else{
                        row.addClass('list-switch-off').find('.layui-icon').html('&#x1006;');
                        row.attr('title','点击关闭');
                    }
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 1, time: 1000});
                }else{
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                }
            });
        });
        //excel导入
        upload.render({
            url: '<?php echo url("upload/upfile") ?>'
            ,elem:'#excel'
            ,before: function(input){
                loading = layer.load(2, {
                    shade: [0.2,'#000'] //0.2透明度的白色背景
                });
            }
            ,success: function(res){
                var category_id = {$da['category_id']|default='0'};
                var path = res.path;
                jq.post('{:url("article/import")}',{'category_id':category_id,'path':path},function(data){
                    if(data.code == 200){
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                            location.reload();//do something
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                    }
                });
            }
        });
        //图片预览
        jq('.list-table td .thumb').hover(function(){
            jq(this).append('<img class="thumb-show" src="'+jq(this).attr('thumb')+'" >');
        },function(){
            jq(this).find('img').remove();
        });
        //ajax删除
        jq('.del_btn').click(function(){
            var name = jq(this).attr('article-name');
            var id = jq(this).attr('article-id');
            layer.confirm('确定删除【'+name+'】?', function(index){
                loading = layer.load(2, {
                    shade: [0.2,'#000'] //0.2透明度的白色背景
                });
                jq.post('{:url("article/is_del")}',{'id':id},function(data){
                    if(data.code == 200){
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                            location.reload();//do something
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                    }
                });
            });

        });
        //全选
        form.on('checkbox(checkAll)', function(data){
            if(data.elem.checked){
                jq("input[type='checkbox']").prop('checked',true);
            }else{
                jq("input[type='checkbox']").prop('checked',false);
            }
            form.render('checkbox');
        });
        form.on('checkbox(checkOne)', function(data){
            var is_check = true;
            if(data.elem.checked){
                jq("input[lay-filter='checkOne']").each(function(){
                    if(!jq(this).prop('checked')){ is_check = false; }
                });
                if(is_check){
                    jq("input[lay-filter='checkAll']").prop('checked',true);
                }
            }else{
                jq("input[lay-filter='checkAll']").prop('checked',false);
            }
            form.render('checkbox');
        });

        //监听删除提交
        form.on('submit(delete)', function(data){
            //判断是否有选项
            var is_check = false;
            jq("input[lay-filter='checkOne']").each(function(){
                if(jq(this).prop('checked')){ is_check = true; }
            });
            if(!is_check){
                layer.msg('请选择数据', {icon: 2,anim: 6,time: 1000});
                return false;
            }
            //确认删除
            layer.confirm('确定批量删除?', function(index){
                loading = layer.load(2, {
                    shade: [0.2,'#000'] //0.2透明度的白色背景
                });
                var param = data.field;
                jq.post('{:url("article/batches_is_delete")}',param,function(data){
                    if(data.code == 200){
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                            location.reload();//do something
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
                    }
                });
            });
            return false;
        });

        //监听移动提交
        form.on('submit(move)', function(data){
            //判断是否有选项
            var is_check = false;
            jq("input[lay-filter='checkOne']").each(function(){
                if(jq(this).prop('checked')){ is_check = true; }
            });
            if(!is_check){
                layer.msg('请选择数据', {icon: 2,anim: 6,time: 1000});
                return false;
            }
            //移动到位置选择 var params = JSON.stringify(data.field);
            loading = layer.load(2, {
                shade: [0.2,'#000'] //0.2透明度的白色背景
            });
            var param = data.field;
            jq.post('{:url("category/model_category_select")}',param,function(data){
                layer.open({
                    type: 1,
                    title: '数据移动',
                    area: ['auto', '490px'],
                    content: data //这里content是一个普通的String
                });
                form.render('select');//刷新重新加载select
                layer.close(loading);
                form.on('select(move)', function(data){
                    param.to_category_id = data.value;
                    var selected_text = jq(data.elem).find("option:selected").text();
                    //确认移动
                    layer.confirm('确定批量移动至【'+selected_text+'】?', function(index){
                        loading = layer.load(2, {
                            shade: [0.2,'#000'] //0.2透明度的白色背景
                        });
                        jq.post('{:url("article/batches_move")}',param,function(data){
                            if(data.code == 200){
                                layer.close(loading);
                                layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                    location.reload();//do something
                                });
                            }else{
                                layer.close(loading);
                                layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
                            }
                        });
                    });
                });
            });

            return false;
        });

        /*laypage({
            cont: 'page'
            ,skip: true
            ,pages: '' //总页数
            ,groups: 5 //连续显示分页数
            ,curr: ''
            ,first: 1 //将首页显示为数字1,。若不显示，设置false即可
            ,last: '' //将尾页显示为总页数。若不显示，设置false即可
            ,jump: function(e, first){ //触发分页后的回调
                if(!first){ //一定要加此判断，否则初始时会无限刷新
                    loading = layer.load(2, {
                        shade: [0.2,'#000'] //0.2透明度的白色背景
                    });
                    location.href = '';
                }
        }*/
    });

</script>
</body>
</html>